<template>
  <div>
    <el-dialog :title="dialogInfo.isAdd ? '添加菜单':'编辑菜单'" :visible.sync="dialogInfo.isShow" :before-close="cancel">
      <el-form :model="menuInfo" :rules="rules" ref="menuInfo">
        <el-form-item label="菜单名称：" :label-width="formLabelWidth" prop="title">
          <el-input v-model="menuInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单：" :label-width="formLabelWidth" prop="pid">
          <el-select v-model="menuInfo.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in menuList" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型：" :label-width="formLabelWidth" prop="type">
          <el-radio v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标：" :label-width="formLabelWidth" prop="icon">
          <el-input v-model="menuInfo.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单地址：" :label-width="formLabelWidth" prop="url">
          <el-input v-model="menuInfo.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状  态：" :label-width="formLabelWidth" prop="status">
          <el-switch
            v-model="menuInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd==true" type="primary" @click="addMenu"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="editMenu"
          >编辑</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addmenu,getmenuinfo, getmenuedit} from '../../util/axios'
import {mapGetters,mapActions} from 'vuex' 
export default {
  data() {
    return {
       formLabelWidth: '100px',
       menuInfo:{
        pid:'',//上级分类编号
        title:'',//标题
        icon:'',//图标
        type:1,//类型1目录2菜单
        url:'',//菜单地址
        status:1
      },
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }]
      },
    };
  },
  props:['dialogInfo'],
  computed:{
    ...mapGetters({
      menuList:'menu/getMenuList'
    })
  },
  methods:{
    ...mapActions({
      getMenuList:'menu/getMenuListAction'
    }),
    cancel(){
      this.$emit('cancel',false)
      this.reset();
    },
    reset(){
      this.menuInfo = {
        pid:'',//上级分类编号
        title:'',//标题
        icon:'',//图标
        type:1,//类型1目录2菜单
        url:'',//菜单地址
        status:1
      }
      this.$refs.menuInfo.resetFields()
    },
    addMenu(){
      this.$refs.menuInfo.validate(valid=>{
        if(valid){
          addmenu(this.menuInfo).then(res=>{
            if(res.data.code==200){
              this.$message.success(res.data.msg)
              this.getMenuList()
            }else{
              this.$message.error(res.data.msg)
            }
          })
        }else{
          this.$message.error('验证失败')
        }
      })
      this.cancel()
      this.reset()
    },
    lookup(id){
      getmenuinfo({id}).then(res=>{
        if(res.data.code==200){
          this.menuInfo = res.data.list
          this.menuInfo.id = id
        }
      })
    },
    editMenu(){
      this.$refs.menuInfo.validate(valid=>{
        if(valid){
          getmenuedit(this.menuInfo).then(res=>{
            if(res.data.code==200){
              this.$message.success('编辑成功')
              this.cancel()
              this.getMenuList()
            }else{
              this.$message.error('编辑失败')
            }
          })
        }else{
          this.$message.error('验证失败')
        }
      })
    }
  }
};
</script>

<style lang="less" scoped>
.el-input{
  width: 90%;
}
</style>
